package com.wz.project.mydo.mydojob.service;

import com.wz.common.bas.taskNoUtils.domain.BasTaskNo;
import com.wz.common.bas.taskNoUtils.mapper.BasTaskNoMapper;
import com.wz.common.utils.text.Convert;
import com.wz.framework.web.domain.AjaxResult;
import com.wz.project.bas.basJobId.domain.BasJobId;
import com.wz.project.bas.basJobId.mapper.BasJobIdMapper;
import com.wz.project.bas.basProductDetail.mapper.BasProductDetailMapper;
import com.wz.project.mydo.domain.MydoJob;
import com.wz.project.mydo.domain.MydoJobs;
import com.wz.project.mydo.mydojobdetail.mapper.JobDetailMapper;
import com.wz.project.mydo.mydojob.mapper.MydoJobMapper;
import com.wz.project.inStock.stockindetail.mapper.InStockinDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.ParameterMode;
import javax.persistence.StoredProcedureQuery;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 作业 服务层实现
 *
 * @author sys
 * @date 2021-10-28
 */
@Service
public class MydoJobServiceImpl implements IMydoJobService {
    @Autowired
    EntityManager entityManager;
    @Autowired
    private MydoJobMapper inMydoJobMapper;

    @Autowired
    private JobDetailMapper detailMapper;

    @Autowired
    private BasProductDetailMapper basProductDetailMapper;

    @Autowired
    private InStockinDetailMapper stockinDetailMapper;
    @Autowired
    private BasJobIdMapper basJobIdMapper;
    @Autowired
    private BasTaskNoMapper basTaskNoMapper;

    /**
     * 查询作业信息
     *
     * @param jobId 作业ID
     * @return 作业信息
     */
    @Override
    public MydoJob selectJobById(String jobId) {
        return inMydoJobMapper.selectJobById(jobId);
    }

    /**
     * 查询作业列表
     *
     * @param inMydoJob 作业信息
     * @return 作业集合
     */
    @Override
    public List<MydoJob> selectJobList(MydoJob inMydoJob) {
        return inMydoJobMapper.selectJobList(inMydoJob);
    }

    /**
     * 新增作业
     *
     * @param inMydoJob 作业信息
     * @return 结果
     */
    @Override
    public int insertJob(MydoJob inMydoJob) {
        return inMydoJobMapper.insertJob(inMydoJob);
    }

    /**
     * 修改作业
     *
     * @param inMydoJob 作业信息
     * @return 结果
     */
    @Override
    public int updateJob(MydoJob inMydoJob) {
        return inMydoJobMapper.updateJob(inMydoJob);
    }

    /**
     * 删除作业对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteJobByIds(String ids) {
        return inMydoJobMapper.deleteJobByIds(Convert.toStrArray(ids));
    }

    @Override
    public int handFinish(String jobId) {
        int sa = 0;
        MydoJob inMydoJob = inMydoJobMapper.selectJobById(jobId);
        if (inMydoJob == null) {
            return sa;
        }
        if (!"3".equals(inMydoJob.getStatus())) {
            return 0;
        }
        String task_no = inMydoJob.getTaskNo();
        StoredProcedureQuery storedProcedureQuery =
                entityManager.createStoredProcedureQuery("IMP_COMPLETE");
        //赋值
        StoredProcedureQuery parameter = storedProcedureQuery
                .registerStoredProcedureParameter("v_task_no", String.class, ParameterMode.IN)
                .registerStoredProcedureParameter("v_result", String.class, ParameterMode.OUT);

        parameter.setParameter("v_task_no", task_no);
        //执行
        storedProcedureQuery.execute();

        //获取返回结果
        String result = (String) storedProcedureQuery.getOutputParameterValue("v_result");
        if ("10".equals(result)) {
            sa = 1;
        } else {
            sa = 2;
        }
        return sa;
    }
    @Override
    public AjaxResult handFinishOut(String jobId) {
        AjaxResult ajaxResult = new AjaxResult();
        MydoJob inMydoJob = inMydoJobMapper.selectJobById(jobId);
        if (inMydoJob == null) {
            ajaxResult.put("msg","无该作业");
            return ajaxResult;
        }
        if (!"3".equals(inMydoJob.getStatus())) {
            ajaxResult.put("msg","未执行的作业无法完成");
            return ajaxResult;
        }
        String task_no = inMydoJob.getTaskNo();
        StoredProcedureQuery storedProcedureQuery =
                entityManager.createStoredProcedureQuery("wcs_in_job_finish");
        //赋值
        StoredProcedureQuery parameter = storedProcedureQuery
                .registerStoredProcedureParameter("v_tray_code", String.class, ParameterMode.IN)
                .registerStoredProcedureParameter("v_result", Integer.class, ParameterMode.OUT)
                .registerStoredProcedureParameter("v_result_msg", String.class, ParameterMode.OUT);

        parameter.setParameter("v_tray_code", inMydoJob.getTrayCode());
        //执行
        storedProcedureQuery.execute();

        //获取返回结果
        String result = (String) storedProcedureQuery.getOutputParameterValue("v_result_msg");
        ajaxResult.put("msg",result);
        return ajaxResult;
    }
    @Override
    public int handInvalid(String jobId) {
        String msg = "";
        StoredProcedureQuery storedProcedureQuery = entityManager.createStoredProcedureQuery("proc_hand_delete_job");
        StoredProcedureQuery parameter = storedProcedureQuery
                .registerStoredProcedureParameter("v_job_id", String.class, ParameterMode.IN)
                .registerStoredProcedureParameter("v_result", Integer.class, ParameterMode.OUT);

        parameter.setParameter("v_job_id", jobId);

        //执行
        storedProcedureQuery.execute();
        //获取返回结果
        int v_result = (Integer) storedProcedureQuery.getOutputParameterValue("v_result");
        return v_result;
    }

    @Override
    public int changeJobStatus(String jobIds[]) {
        for (String jobId : jobIds) {
            MydoJob inMydoJob = inMydoJobMapper.selectJobById(jobId);
            if (inMydoJob == null) {
                return 0;
            }
            if ("1".equals(inMydoJob.getStatus())) {
                inMydoJobMapper.updateStatus(jobId, "3");
                return 1;
            } else if ("3".equals(inMydoJob.getStatus())) {
                inMydoJobMapper.updateStatus(jobId, "1");
                return 1;
            }
            return 0;
        }
        return 0;

    }

    @Override
    public int changeJobpriority(String jobIds[], int a) {
        int res = 0;
        int priority = 10;
        for (String jobId : jobIds) {
            MydoJob mydoJob = inMydoJobMapper.selectJobById(jobId);
            if (mydoJob != null) {
                if ("1".equals(mydoJob.getStatus())) {
                    priority = mydoJob.getPriority();
                    if (a == 1) {
                        priority = priority - 1;
                    } else if (a == 2) {
                        priority = priority + 1;
                    }
                    inMydoJobMapper.updatepriority(jobId, priority);
                    res = 1;
                }
            }
        }
        return res;
    }

    @Override
    public String getJobId(String type) {


        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String date = format.format(new Date());
        int count = basJobIdMapper.getIdByDate(type, date);
        String jobId = "";
        if (count > 0) {
            String jobIdPre = basJobIdMapper.getId(type);
            String[] split = jobIdPre.split(date);
            int i = Integer.parseInt(split[1]);
            if (i > 9999) {
                i = 1;
            }
            String num = String.format("%04d", ++i);
            jobId = type + date + num;
            BasJobId basJobId = new BasJobId();
            basJobId.setJobId(jobId);
            basJobId.setType(type);
            basJobIdMapper.updateBasJobId(basJobId);
        } else {
            int i = 1;
            String num = String.format("%04d", i);
            jobId = type + date + num;
            BasJobId basJobId = new BasJobId();
            basJobId.setJobId(jobId);
            basJobId.setType(type);
            basJobIdMapper.updateBasJobId(basJobId);

        }
        return jobId;
    }


    @Override
    public String getTaskNo(String id) {


        long taskNo = basTaskNoMapper.selectBasTaskNoById(id).getTaskNo();
        long MaxNo = basTaskNoMapper.selectBasTaskNoById(id).getMaxNo();
        if (taskNo == MaxNo) {
            taskNo = basTaskNoMapper.selectBasTaskNoById(id).getMinNo();
        }
        String taskNo1 = String.valueOf(taskNo + 1);
        BasTaskNo basTaskNo = new BasTaskNo();
        basTaskNo.setTaskNo(taskNo + 1);
        basTaskNo.setId(id);
        basTaskNoMapper.updateBasTaskNo(basTaskNo);


        return taskNo1;
    }

    @Override
    public MydoJob selectJobByTask(String s) {

        return inMydoJobMapper.selectJobByTask(s);
    }

    @Override
    public List<MydoJob> selectMydoJobTaskNo(MydoJob mydoJob) {
        return inMydoJobMapper.selectMydoJobTaskNo(mydoJob);
    }

    /**
     * 查询作业表
     *
     * @param jobId 作业表主键
     * @return 作业表
     */
    @Override
    public MydoJob selectMydoJobByJobId(String jobId) {
        return inMydoJobMapper.selectMydoJobByJobId(jobId);
    }

    @Override
    public int updateMydoJob(MydoJob mydoJob) {
        return inMydoJobMapper.updateJob1(mydoJob);
    }
}
